package com.sjh.sjhliving.commodity.service.impl;

import com.sjh.sjhliving.commodity.entity.AttrEntity;
import com.sjh.sjhliving.commodity.service.AttrService;
import com.sjh.sjhliving.commodity.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjh.common.utils.PageUtils;
import com.sjh.common.utils.Query;

import com.sjh.sjhliving.commodity.dao.AttrgroupDao;
import com.sjh.sjhliving.commodity.entity.AttrgroupEntity;
import com.sjh.sjhliving.commodity.service.AttrgroupService;

import javax.annotation.Resource;


@Service("attrgroupService")
public class AttrgroupServiceImpl extends ServiceImpl<AttrgroupDao, AttrgroupEntity> implements AttrgroupService {

    @Resource
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                new QueryWrapper<AttrgroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {

        //先获检索传入的关键字
        String key = (String) params.get("key");
        //根据实际情况,封装查询条件到 QueryWrapper对象
        QueryWrapper<AttrgroupEntity> wrapper = new QueryWrapper<>();
        //判断key是否携带的有查询条件-希望他是一组独立检索条件
        if(StringUtils.isNotBlank(key)){
            wrapper.and((obj)->{
                obj.eq("id", key).or().like("name", key);
            });
        }
        //下面在处理是否需要封装categoryId检索条件
        //我们这里先设置一个业务规则:categoryId==0 表示在查询属性组的时候, 不加入categoryId
        //否则就加入 And categoryId=xx, 这里逻辑需求前端代码配合
        if(categoryId != 0){
            wrapper.eq("category_id", categoryId);
        }

        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                wrapper
        );


        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        //1、查询分组信息
        List<AttrgroupEntity> attrGroupEntities
                = this.list(new QueryWrapper<AttrgroupEntity>().eq("category_id", catelogId));

        //2、查询所有属性
        List<AttrGroupWithAttrsVo> collect
                = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

}